import 'dart:convert';

import 'package:dio/dio.dart';
import 'package:flutter/foundation.dart';
import 'dart:developer';

///日志拦截器
class LogInterceptors extends InterceptorsWrapper {
  @override
  void onRequest(RequestOptions options,RequestInterceptorHandler handler) {
    if (kDebugMode) {
      log("【请求baseUrl】${options.baseUrl}");
      log("【请求path】${options.path}");
      log("【请求headers】${options.headers.toString()}");
      log("【请求参数】${options.data.toString()}");
      log("【请求参数】${options.queryParameters.toString()}");
    }
    handler.next(options);
  }

  @override
  void onResponse(Response response, ResponseInterceptorHandler handler) {
    if (kDebugMode) {
      log("【返回statusCode】${response.statusCode}");
      log("【返回headers】${response.headers.toString()}");
      log("【返回extra】${response.extra.toString()}");
      log("【返回data】${json.encode(response.data)}");
    }
    handler.next(response);
  }

  @override
  void onError(DioError err, ErrorInterceptorHandler handler) {
    if (kDebugMode) {
      log(err.toString());
      log(err.response?.toString()??"");
    }
    return super.onError(err,handler);
  }
}